package cn.sfrog.project.biz.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.project.biz.admin.domain.BizServiceUser;
import cn.sfrog.project.biz.admin.service.IBizComboService;
import cn.sfrog.project.biz.admin.service.IBizServiceOrderService;
import cn.sfrog.project.biz.admin.service.IBizServiceService;
import cn.sfrog.project.biz.web.vo.service.AppServiceDetailQueryVO;
import cn.sfrog.project.biz.web.vo.service.AppServiceDetailResultVO;
import cn.sfrog.system.domain.vo.app.*;
import cn.sfrog.system.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/api/escort")
@Api(value = "/api/escort", tags = "APP陪护相关")
public class AppEscortController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBizServiceService bizServiceService;
    @Autowired
    private IBizComboService bizComboService;
    @Autowired
    private IBizServiceOrderService bizServiceOrderService;

    @ApiOperation(value = "获取陪护列表")
    @GetMapping("/getEscortList")
    public TableDataInfo<List<AppEscortResultVO>> getEscortList(AppEscortQueryVO queryVO){
        startPage();
        List<AppEscortResultVO> escortResultVOList = sysUserService.selectAppEscortList(queryVO);
        return getDataTable(escortResultVOList);
    }

    @ApiOperation(value = "获取陪护详情")
    @GetMapping("/detail/{userId}")
    public ApiAjaxResult<AppEscortDetailResultVO> getEscortDetail(@PathVariable("userId") Long userId){
        AppEscortDetailResultVO resultVO = sysUserService.selectAppEscortDetail(userId);
        List<AppServiceResultVO> serviceList = new ArrayList<>();
        //查询这个陪护有的服务
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getUserId,userId)
                .eq(BizServiceUser::getStatus,BizServiceUser.STATUS_UP);
        List<BizServiceUser> serviceUserList = BizServiceUser.DAO.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(serviceUserList)){
            List<BizServiceUser> serviceUsers = serviceUserList.stream().filter(x -> x.getType().equals(Constants.TYPE_SERVICE)).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(serviceUsers)){
                List<Long> serviceIds = serviceUsers.stream().map(item -> item.getProductId()).collect(Collectors.toList());
                AppServiceQueryVO appServiceQueryVO = new AppServiceQueryVO();
                appServiceQueryVO.setIds(serviceIds);
                appServiceQueryVO.setCurrentUserId(userId);
                serviceList = bizServiceService.selectAppServiceList(appServiceQueryVO);
            }
            List<BizServiceUser> comboUsers = serviceUserList.stream().filter(x -> x.getType().equals(Constants.TYPE_COMBO)).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(comboUsers)){
                List<Long> comboIds = comboUsers.stream().map(item -> item.getProductId()).collect(Collectors.toList());
                AppComboQueryVO appComboQueryVO = new AppComboQueryVO();
                appComboQueryVO.setIds(comboIds);
                appComboQueryVO.setCurrentUserId(userId);
                List<AppServiceResultVO> comboList = bizComboService.selectAppComboList(appComboQueryVO);
                serviceList.addAll(comboList);
            }
            resultVO.setServiceList(serviceList);
        }
        return ApiAjaxResult.success(resultVO);
    }

    @ApiOperation(value = "获取服务|套餐详情")
    @GetMapping("/serviceDetail")
    public ApiAjaxResult<AppServiceDetailResultVO> getServiceDetail(AppServiceDetailQueryVO queryVO){
        AppServiceDetailResultVO resultVO = bizServiceService.selectAppServiceDetail(queryVO);
        return ApiAjaxResult.success(resultVO);
    }

}
